using System;
using System.Collections;
using System.Collections.Generic;
using ToffeeHttp.Runtime.Core;
using UnityEngine;
using UnityEngine.Networking;

namespace ToffeeHttp.Runtime
{
    public class GetHandlerFrom : IRequestHandler
    {

        public UnityWebRequest RequestCreate(Uri uri, object data)
        {
            string st = "";
            if (data != null)
            {
                if (data is WWWForm)
                {
                    
                    var td   = (WWWForm) data;
                    var keys = td.headers.Keys;
                    if (keys.Count > 0)
                    {
                        st = "?";
                        foreach (var o in keys)
                        {
                            st += $"{o}={td.headers[o]}&";
                        }
                        st.Remove(st.Length - 1);
                    }
                }
                else if (data is IDictionary<string, object>)
                {
                    
                    var td = (IDictionary<string, object>) data;
                    var tkeys = td.Keys;
                    if (tkeys.Count > 0)
                    {
                        st = "?";
                        foreach (var o in tkeys)
                        {
                            st += $"{o}={td[o]}&";
                        }
                        st.Remove(st.Length - 1);
                    }
                }
            }
            UnityWebRequest requestU = UnityWebRequest.Get(uri.OriginalString + st);
            return requestU;
        }

    }
}